import React, { useRef } from 'react';
import { Text, Box, Cylinder, Sphere, Plane, RoundedBox, Cone } from '@react-three/drei';
import * as THREE from 'three';

interface UltraRestaurantEnvironmentProps {
  radius?: number;
}

export const UltraRestaurantEnvironment: React.FC<UltraRestaurantEnvironmentProps> = ({ 
  radius = 18 
}) => {
  const envRef = useRef<THREE.Group>(null);
  const GROUND_LEVEL = 0;

  // 高端餐厅材质系统
  const materials = {
    floor: { color: '#2F1B14', roughness: 0.2, metalness: 0.1 },
    wall: { color: '#8B4513', roughness: 0.6, metalness: 0.0 },
    ceiling: { color: '#F5F5DC', roughness: 0.4, metalness: 0.0 },
    wood: { color: '#654321', roughness: 0.7, metalness: 0.0 },
    marble: { color: '#F8F8FF', roughness: 0.2, metalness: 0.3 },
    gold: { color: '#FFD700', roughness: 0.1, metalness: 0.9 },
    crystal: { color: '#FFFFFF', roughness: 0.0, metalness: 0.9 },
    leather: { color: '#8B0000', roughness: 0.4, metalness: 0.1 },
    brass: { color: '#B8860B', roughness: 0.3, metalness: 0.8 },
    fabric: { color: '#DC143C', roughness: 0.8, metalness: 0.0 }
  };

  return (
    <group ref={envRef}>
      {/* ========== 豪华地面系统 ========== */}
      
      {/* 主地面 - 深色木纹 */}
      <Plane 
        args={[radius * 2, radius * 2]} 
        rotation={[-Math.PI / 2, 0, 0]}
        position={[0, GROUND_LEVEL, 0]}
        receiveShadow
      >
        <meshStandardMaterial {...materials.floor} />
      </Plane>

      {/* 地板拼花图案 */}
      {Array.from({ length: 12 }, (_, i) => {
        const angle = (i * Math.PI * 2) / 12;
        const x = Math.cos(angle) * 8;
        const z = Math.sin(angle) * 8;
        return (
          <RoundedBox
            key={i}
            position={[x, GROUND_LEVEL + 0.002, z]}
            args={[1.5, 0.005, 1.5]}
            radius={0.1}
            receiveShadow
          >
            <meshStandardMaterial color="#8B4513" roughness={0.3} metalness={0.2} />
          </RoundedBox>
        );
      })}

      {/* 中央地毯 */}
      <Cylinder
        position={[0, GROUND_LEVEL + 0.005, 0]}
        args={[6, 6, 0.02]}
        receiveShadow
      >
        <meshStandardMaterial {...materials.fabric} />
      </Cylinder>

      {/* ========== 优雅墙面系统 ========== */}
      
      {/* 四面墙壁 */}
      <Plane args={[radius * 2, 5]} position={[0, 2.5, -radius]} receiveShadow>
        <meshStandardMaterial {...materials.wall} />
      </Plane>
      <Plane args={[radius * 2, 5]} position={[0, 2.5, radius]} rotation={[0, Math.PI, 0]} receiveShadow>
        <meshStandardMaterial {...materials.wall} />
      </Plane>
      <Plane args={[radius * 2, 5]} position={[-radius, 2.5, 0]} rotation={[0, Math.PI / 2, 0]} receiveShadow>
        <meshStandardMaterial {...materials.wall} />
      </Plane>
      <Plane args={[radius * 2, 5]} position={[radius, 2.5, 0]} rotation={[0, -Math.PI / 2, 0]} receiveShadow>
        <meshStandardMaterial {...materials.wall} />
      </Plane>

      {/* 墙面护墙板装饰 */}
      {[
        { pos: [0, 1, -radius + 0.1], rot: [0, 0, 0] },
        { pos: [0, 1, radius - 0.1], rot: [0, Math.PI, 0] },
        { pos: [-radius + 0.1, 1, 0], rot: [0, Math.PI / 2, 0] },
        { pos: [radius - 0.1, 1, 0], rot: [0, -Math.PI / 2, 0] }
      ].map((wall, i) => (
        <group key={`wall-panel-${i}`} position={wall.pos} rotation={wall.rot}>
          <RoundedBox args={[16, 2, 0.15]} radius={0.05} castShadow>
            <meshStandardMaterial {...materials.wood} />
          </RoundedBox>
          
          {/* 装饰线条 */}
          <RoundedBox position={[0, 0.8, 0.08]} args={[15, 0.08, 0.05]} radius={0.02} castShadow>
            <meshStandardMaterial {...materials.gold} />
          </RoundedBox>
          <RoundedBox position={[0, -0.8, 0.08]} args={[15, 0.08, 0.05]} radius={0.02} castShadow>
            <meshStandardMaterial {...materials.gold} />
          </RoundedBox>
        </group>
      ))}

      {/* 奢华天花板 */}
      <Plane args={[radius * 2, radius * 2]} rotation={[Math.PI / 2, 0, 0]} position={[0, 5, 0]}>
        <meshStandardMaterial {...materials.ceiling} />
      </Plane>

      {/* 天花板雕花装饰 */}
      <Cylinder position={[0, 4.9, 0]} args={[8, 8, 0.1]} castShadow>
        <meshStandardMaterial {...materials.gold} />
      </Cylinder>

      {/* ========== 中央吧台系统 ========== */}
      
      <group position={[0, GROUND_LEVEL, radius - 5]}>
        {/* 主吧台 - 弧形设计 */}
        <Cylinder position={[0, 0.7, 0]} args={[6, 6, 1.4, 16]} castShadow receiveShadow>
          <meshStandardMaterial {...materials.wood} />
        </Cylinder>
        
        {/* 大理石台面 */}
        <Cylinder position={[0, 1.45, 0]} args={[6.3, 6.3, 0.1, 16]} castShadow>
          <meshStandardMaterial {...materials.marble} />
        </Cylinder>
        
        {/* 台面装饰边缘 */}
        <Cylinder position={[0, 1.52, 0]} args={[6.4, 6.4, 0.04, 16]} castShadow>
          <meshStandardMaterial {...materials.gold} />
        </Cylinder>

        {/* 酒柜背景墙 */}
        <Cylinder position={[0, 3, -5.5]} args={[7, 7, 0.3, 16]} castShadow>
          <meshStandardMaterial {...materials.wood} />
        </Cylinder>
        
        {/* 酒瓶展示 */}
        {Array.from({ length: 24 }, (_, i) => {
          const ringIndex = Math.floor(i / 8);
          const itemInRing = i % 8;
          const angle = (itemInRing * Math.PI * 2) / 8;
          const r = 3 + ringIndex * 1.2;
          const x = Math.cos(angle) * r;
          const z = -5.5 + Math.sin(angle) * 0.3;
          const y = 2 + ringIndex * 0.8;
          
          return (
            <group key={`bottle-${i}`} position={[x, y, z]}>
              <Cylinder args={[0.04, 0.04, 0.6]} castShadow>
                <meshStandardMaterial 
                  color={i % 3 === 0 ? "#2F4F4F" : i % 3 === 1 ? "#8B4513" : "#006400"}
                  transparent
                  opacity={0.8}
                />
              </Cylinder>
              <Cylinder position={[0, 0.35, 0]} args={[0.05, 0.05, 0.1]} castShadow>
                <meshStandardMaterial {...materials.gold} />
              </Cylinder>
            </group>
          );
        })}

        {/* 专业调酒设备 */}
        <group position={[3, 1.5, 0]}>
          <RoundedBox args={[1, 0.8, 0.6]} radius={0.05} castShadow>
            <meshStandardMaterial {...materials.brass} />
          </RoundedBox>
          
          {/* 调酒器具 */}
          <Cylinder position={[-0.3, 0.5, 0]} args={[0.06, 0.06, 0.4]} castShadow>
            <meshStandardMaterial {...materials.crystal} />
          </Cylinder>
          <Cylinder position={[0.3, 0.5, 0]} args={[0.08, 0.08, 0.3]} castShadow>
            <meshStandardMaterial {...materials.brass} />
          </Cylinder>
        </group>
      </group>

      {/* ========== 豪华用餐区域 ========== */}
      
      {/* VIP包厢 */}
      {[
        { pos: [-12, 0, -8], rot: 0 },
        { pos: [12, 0, -8], rot: Math.PI },
        { pos: [-12, 0, 8], rot: 0 },
        { pos: [12, 0, 8], rot: Math.PI }
      ].map((booth, i) => (
        <group key={`vip-booth-${i}`} position={booth.pos} rotation={[0, booth.rot, 0]}>
          {/* 包厢隔断 */}
          <RoundedBox position={[0, 1.2, -1.5]} args={[4, 2.4, 0.2]} radius={0.1} castShadow>
            <meshStandardMaterial {...materials.wood} />
          </RoundedBox>
          <RoundedBox position={[-1.8, 1.2, 0]} args={[0.2, 2.4, 3]} radius={0.1} castShadow>
            <meshStandardMaterial {...materials.wood} />
          </RoundedBox>
          <RoundedBox position={[1.8, 1.2, 0]} args={[0.2, 2.4, 3]} radius={0.1} castShadow>
            <meshStandardMaterial {...materials.wood} />
          </RoundedBox>
          
          {/* 豪华U型卡座 */}
          <group>
            {/* 后座 */}
            <RoundedBox position={[0, 0.3, -1]} args={[3.5, 0.6, 0.8]} radius={0.1} castShadow receiveShadow>
              <meshStandardMaterial {...materials.leather} />
            </RoundedBox>
            <RoundedBox position={[0, 0.8, -1.3]} args={[3.5, 1, 0.2]} radius={0.1} castShadow>
              <meshStandardMaterial {...materials.leather} />
            </RoundedBox>
            
            {/* 左侧座 */}
            <RoundedBox position={[-1.5, 0.3, 0]} args={[0.8, 0.6, 2]} radius={0.1} castShadow receiveShadow>
              <meshStandardMaterial {...materials.leather} />
            </RoundedBox>
            <RoundedBox position={[-1.8, 0.8, 0]} args={[0.2, 1, 2]} radius={0.1} castShadow>
              <meshStandardMaterial {...materials.leather} />
            </RoundedBox>
            
            {/* 右侧座 */}
            <RoundedBox position={[1.5, 0.3, 0]} args={[0.8, 0.6, 2]} radius={0.1} castShadow receiveShadow>
              <meshStandardMaterial {...materials.leather} />
            </RoundedBox>
            <RoundedBox position={[1.8, 0.8, 0]} args={[0.2, 1, 2]} radius={0.1} castShadow>
              <meshStandardMaterial {...materials.leather} />
            </RoundedBox>
          </group>
          
          {/* 大理石餐桌 */}
          <RoundedBox position={[0, 0.4, 0.3]} args={[2.5, 0.08, 1.5]} radius={0.05} castShadow receiveShadow>
            <meshStandardMaterial {...materials.marble} />
          </RoundedBox>
          
          {/* 桌腿 - 雕刻装饰 */}
          <Cylinder position={[0, 0.2, 0.3]} args={[0.1, 0.1, 0.4]} castShadow>
            <meshStandardMaterial {...materials.gold} />
          </Cylinder>
          
          {/* 桌面装饰 */}
          <group position={[0, 0.45, 0.3]}>
            {/* 餐具摆设 */}
            <Cylinder position={[-0.6, 0.01, 0.4]} args={[0.15, 0.15, 0.02]} castShadow>
              <meshStandardMaterial color="#F5F5DC" />
            </Cylinder>
            <Cylinder position={[0.6, 0.01, 0.4]} args={[0.15, 0.15, 0.02]} castShadow>
              <meshStandardMaterial color="#F5F5DC" />
            </Cylinder>
            
            {/* 酒杯 */}
            <Cylinder position={[-0.3, 0.04, 0.2]} args={[0.03, 0.03, 0.08]} castShadow>
              <meshStandardMaterial {...materials.crystal} />
            </Cylinder>
            <Cylinder position={[0.3, 0.04, 0.2]} args={[0.03, 0.03, 0.08]} castShadow>
              <meshStandardMaterial {...materials.crystal} />
            </Cylinder>
            
            {/* 中央装饰花瓶 */}
            <Cylinder position={[0, 0.08, -0.2]} args={[0.06, 0.08, 0.16]} castShadow>
              <meshStandardMaterial {...materials.crystal} />
            </Cylinder>
            
            {/* 蜡烛台 */}
            <group position={[0, 0.05, 0]}>
              <Cylinder args={[0.04, 0.04, 0.1]} castShadow>
                <meshStandardMaterial {...materials.brass} />
              </Cylinder>
              <Cylinder position={[0, 0.08, 0]} args={[0.008, 0.008, 0.06]} castShadow>
                <meshStandardMaterial color="#FFFACD" />
              </Cylinder>
              <Sphere position={[0, 0.14, 0]} args={[0.015]} castShadow>
                <meshBasicMaterial color="#FF4500" />
              </Sphere>
            </group>
          </group>
          
          {/* 包厢内吊灯 */}
          <group position={[0, 3.8, 0]}>
            <Cylinder args={[0.3, 0.2, 0.6]} castShadow>
              <meshStandardMaterial {...materials.brass} />
            </Cylinder>
            
            {Array.from({ length: 8 }, (_, j) => {
              const angle = (j * Math.PI * 2) / 8;
              return (
                <Cylinder
                  key={j}
                  position={[
                    Math.cos(angle) * 0.4,
                    -0.4,
                    Math.sin(angle) * 0.4
                  ]}
                  args={[0.02, 0.02, 0.3]}
                  castShadow
                >
                  <meshStandardMaterial {...materials.crystal} />
                </Cylinder>
              );
            })}
            
            <pointLight
              intensity={1.5}
              distance={8}
              color="#FFFACD"
              castShadow
            />
          </group>
        </group>
      ))}

      {/* 中央用餐区圆桌 */}
      {[
        { pos: [-6, 0, -3] },
        { pos: [6, 0, -3] },
        { pos: [-6, 0, 3] },
        { pos: [6, 0, 3] },
        { pos: [0, 0, 0] }
      ].map((table, i) => (
        <group key={`central-table-${i}`} position={table.pos}>
          {/* 大理石圆桌面 */}
          <Cylinder position={[0, 0.45, 0]} args={[1.2, 1.2, 0.08]} castShadow receiveShadow>
            <meshStandardMaterial {...materials.marble} />
          </Cylinder>
          
          {/* 桌面金边装饰 */}
          <Cylinder position={[0, 0.49, 0]} args={[1.25, 1.25, 0.02]} castShadow>
            <meshStandardMaterial {...materials.gold} />
          </Cylinder>
          
          {/* 华丽桌腿 */}
          <group position={[0, 0.225, 0]}>
            <Cylinder args={[0.08, 0.1, 0.45]} castShadow>
              <meshStandardMaterial {...materials.gold} />
            </Cylinder>
            
            {/* 桌腿装饰环 */}
            {[0.1, 0.2, 0.3].map((height, ringIndex) => (
              <Cylinder
                key={ringIndex}
                position={[0, height - 0.225, 0]}
                args={[0.12, 0.12, 0.03]}
                castShadow
              >
                <meshStandardMaterial {...materials.brass} />
              </Cylinder>
            ))}
          </group>

          {/* 高档餐椅 */}
          {[0, Math.PI / 2, Math.PI, (3 * Math.PI) / 2].map((angle, chairIndex) => {
            const chairX = Math.cos(angle) * 1.6;
            const chairZ = Math.sin(angle) * 1.6;
            
            return (
              <group 
                key={chairIndex} 
                position={[chairX, GROUND_LEVEL, chairZ]}
                rotation={[0, angle + Math.PI, 0]}
              >
                {/* 椅座 */}
                <RoundedBox position={[0, 0.25, 0]} args={[0.5, 0.08, 0.5]} radius={0.05} castShadow receiveShadow>
                  <meshStandardMaterial {...materials.leather} />
                </RoundedBox>
                
                {/* 高椅背 */}
                <RoundedBox position={[0, 0.7, -0.2]} args={[0.5, 0.9, 0.08]} radius={0.05} castShadow>
                  <meshStandardMaterial {...materials.leather} />
                </RoundedBox>
                
                {/* 椅背雕花 */}
                <RoundedBox position={[0, 0.9, -0.16]} args={[0.3, 0.2, 0.04]} radius={0.02} castShadow>
                  <meshStandardMaterial {...materials.gold} />
                </RoundedBox>
                
                {/* 椅腿 - 弧形设计 */}
                {[[-0.18, -0.18], [0.18, -0.18], [-0.18, 0.18], [0.18, 0.18]].map((legPos, legIndex) => (
                  <Cylinder
                    key={legIndex}
                    position={[legPos[0], 0.125, legPos[1]]}
                    args={[0.025, 0.025, 0.25]}
                    castShadow
                  >
                    <meshStandardMaterial {...materials.gold} />
                  </Cylinder>
                ))}
                
                {/* 扶手 */}
                <RoundedBox position={[-0.28, 0.4, 0]} args={[0.08, 0.06, 0.3]} radius={0.03} castShadow>
                  <meshStandardMaterial {...materials.wood} />
                </RoundedBox>
                <RoundedBox position={[0.28, 0.4, 0]} args={[0.08, 0.06, 0.3]} radius={0.03} castShadow>
                  <meshStandardMaterial {...materials.wood} />
                </RoundedBox>
              </group>
            );
          })}
        </group>
      ))}

      {/* ========== 豪华装饰系统 ========== */}
      
      {/* 主吊灯 */}
      <group position={[0, 4.5, 0]}>
        {/* 主灯体 */}
        <Cylinder args={[0.6, 0.4, 1.5]} castShadow>
          <meshStandardMaterial {...materials.gold} />
        </Cylinder>
        
        {/* 水晶装饰环 */}
        {Array.from({ length: 3 }, (_, ring) => (
          <group key={ring} position={[0, -0.5 - ring * 0.3, 0]}>
            {Array.from({ length: 12 }, (_, crystal) => {
              const angle = (crystal * Math.PI * 2) / 12;
              const r = 0.8 + ring * 0.3;
              return (
                <group
                  key={crystal}
                  position={[
                    Math.cos(angle) * r,
                    0,
                    Math.sin(angle) * r
                  ]}
                >
                  <Cylinder args={[0.02, 0.01, 0.4]} castShadow>
                    <meshStandardMaterial {...materials.crystal} />
                  </Cylinder>
                  <Sphere position={[0, -0.25, 0]} args={[0.04]} castShadow>
                    <meshStandardMaterial 
                      color="#FFFFFF" 
                      transparent 
                      opacity={0.9}
                      roughness={0.0}
                      metalness={0.9}
                    />
                  </Sphere>
                </group>
              );
            })}
          </group>
        ))}
        
        <pointLight
          intensity={2}
          distance={20}
          color="#FFFACD"
          castShadow
          shadow-mapSize={[1024, 1024]}
        />
      </group>

      {/* 壁画装饰 */}
      {[
        { pos: [-radius + 0.1, 3, 0], rot: [0, Math.PI / 2, 0] },
        { pos: [radius - 0.1, 3, 0], rot: [0, -Math.PI / 2, 0] }
      ].map((art, i) => (
        <group key={`wall-art-${i}`} position={art.pos} rotation={art.rot}>
          <RoundedBox args={[3, 2, 0.15]} radius={0.1} castShadow>
            <meshStandardMaterial {...materials.gold} />
          </RoundedBox>
          <Plane args={[2.5, 1.6]} position={[0, 0, 0.08]}>
            <meshStandardMaterial color="#8B0000" />
          </Plane>
        </group>
      ))}

      {/* 落地花瓶装饰 */}
      {[
        [-14, 0, -14], [14, 0, -14], [-14, 0, 14], [14, 0, 14]
      ].map((pos, i) => (
        <group key={`vase-${i}`} position={pos}>
          <Cylinder position={[0, 0.6, 0]} args={[0.3, 0.2, 1.2]} castShadow>
            <meshStandardMaterial {...materials.crystal} />
          </Cylinder>
          
          {/* 装饰花朵 */}
          {Array.from({ length: 5 }, (_, j) => (
            <Sphere
              key={j}
              position={[
                (Math.random() - 0.5) * 0.4,
                1.2 + j * 0.1,
                (Math.random() - 0.5) * 0.4
              ]}
              args={[0.04]}
              castShadow
            >
              <meshStandardMaterial 
                color={["#FF1493", "#FF69B4", "#FFA500", "#FFFF00", "#FF6347"][j]} 
              />
            </Sphere>
          ))}
        </group>
      ))}

      {/* ========== 专业照明系统 ========== */}
      
      <ambientLight intensity={0.3} color="#FFFACD" />
      
      <directionalLight 
        position={[15, 20, 15]} 
        intensity={1} 
        color="#FFFACD"
        castShadow
        shadow-mapSize={[2048, 2048]}
        shadow-camera-far={50}
        shadow-camera-left={-25}
        shadow-camera-right={25}
        shadow-camera-top={25}
        shadow-camera-bottom={-25}
      />
      
      <pointLight 
        position={[-10, 8, -10]} 
        intensity={0.8} 
        distance={15}
        color="#FFE4B5"
      />
      
      <pointLight 
        position={[10, 8, 10]} 
        intensity={0.8} 
        distance={15}
        color="#F0E68C"
      />
      
      {/* 边界检测 */}
      <Sphere args={[radius]} position={[0, 0, 0]}>
        <meshBasicMaterial 
          transparent 
          opacity={0}
          side={THREE.BackSide}
        />
      </Sphere>
    </group>
  );
};